const koa = require('koa');
const app = new koa();

// 解决跨域问题
const cors = require('koa2-cors');
app.use(
  cors({
    origin: ['http://localhost:8000'], // 允许跨域的地址
    credentials: true, // 证书
  })
);

// 接受前端get请求
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());

// 加载路由
const Router = require('koa-router');
let user = require('./controller/user.js');

let router = new Router();
router.use('/user', user.routes());

app.use(router.routes());
app.use(router.allowedMethods());

const { connect, initSchemas } = require('./init');

(async () => {
  // 连接数据库
  await connect();
  // 初始化模型
  initSchemas();
})();

app.use(async (ctx) => {
  ctx.body = 'hello';
});

app.listen(3000, () => {
  console.log('server run success');
});
